home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / louie / plugin.py < prev    next >
Text File  |  2006-01-30  |  3KB  |  109 lines

  1. """Common plugins for Louie."""
  2.  
  3. from louie import dispatcher
  4. from louie import error
  5.  
  6.  
  7. def install_plugin(plugin):
  8.     cls = plugin.__class__
  9.     for p in dispatcher.plugins:
  10.         if p.__class__ is cls:
  11.             raise error.PluginTypeError(
  12.                 'Plugin of type %r already installed.' % cls)
  13.     dispatcher.plugins.append(plugin)
  14.  
  15. def remove_plugin(plugin):
  16.     dispatcher.plugins.remove(plugin)
  17.  
  18.  
  19. class Plugin(object):
  20.     """Base class for Louie plugins.
  21.  
  22.     Plugins are used to extend or alter the behavior of Louie
  23.     in a uniform way without having to modify the Louie code
  24.     itself.
  25.     """
  26.  
  27.     def is_live(self, receiver):
  28.         """Return True if the receiver is still live.
  29.  
  30.         Only called for receivers who have already been determined to
  31.         be live by default Louie semantics.
  32.         """
  33.         return True
  34.  
  35.     def wrap_receiver(self, receiver):
  36.         """Return a callable that passes arguments to the receiver.
  37.  
  38.         Useful when you want to change the behavior of all receivers.
  39.         """
  40.         return receiver
  41.  
  42.  
  43. class QtWidgetPlugin(Plugin):
  44.     """A Plugin for Louie that knows how to handle Qt widgets
  45.     when using PyQt built with SIP 4 or higher.
  46.  
  47.     Weak references are not useful when dealing with QWidget
  48.     instances, because even after a QWidget is closed and destroyed,
  49.     only the C++ object is destroyed.  The Python 'shell' object
  50.     remains, but raises a RuntimeError when an attempt is made to call
  51.     an underlying QWidget method.
  52.  
  53.     This plugin alleviates this behavior, and if a QWidget instance is
  54.     found that is just an empty shell, it prevents Louie from
  55.     dispatching to any methods on those objects.
  56.     """
  57.  
  58.     def __init__(self):
  59.         try:
  60.             import qt
  61.         except ImportError:
  62.             self.is_live = self._is_live_no_qt
  63.         else:
  64.             self.qt = qt
  65.  
  66.     def is_live(self, receiver):
  67.         """If receiver is a method on a QWidget, only return True if
  68.         it hasn't been destroyed."""
  69.         if (hasattr(receiver, 'im_self') and
  70.             isinstance(receiver.im_self, self.qt.QWidget)
  71.             ):
  72.             try:
  73.                 receiver.im_self.x()
  74.             except RuntimeError:
  75.                 return False
  76.         return True
  77.  
  78.     def _is_live_no_qt(self, receiver):
  79.         return True
  80.  
  81.  
  82. class TwistedDispatchPlugin(Plugin):
  83.     """Plugin for Louie that wraps all receivers in callables
  84.     that return Twisted Deferred objects.
  85.  
  86.     When the wrapped receiver is called, it adds a call to the actual
  87.     receiver to the reactor event loop, and returns a Deferred that is
  88.     called back with the result.
  89.     """
  90.  
  91.     def __init__(self):
  92.         # Don't import reactor ourselves, but make access to it
  93.         # easier.
  94.         from twisted import internet
  95.         from twisted.internet.defer import Deferred
  96.         self._internet = internet
  97.         self._Deferred = Deferred
  98.  
  99.     def wrap_receiver(self, receiver):
  100.         def wrapper(*args, **kw):
  101.             d = self._Deferred()
  102.             def called(dummy):
  103.                 return receiver(*args, **kw)
  104.             d.addCallback(called)
  105.             self._internet.reactor.callLater(0, d.callback, None)
  106.             return d
  107.         return wrapper
  108.  
  109.